package cn.sh.yhk.acl.controller;

import cn.sh.yhk.acl.jwt.JwtTokenUtil;
import cn.sh.yhk.acl.po.PermissionPo;
import cn.sh.yhk.acl.service.PermissionService;
import cn.sh.yhk.acl.service.UserInfoService;
import cn.sh.yhk.acl.vo.PermissionVo;
import cn.sh.yhk.acl.vo.UserInfoPageVo;
import cn.sh.yhk.acl.vo.UserInfoVo;
import cn.sh.yhk.base.vo.BaseResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static cn.sh.yhk.acl.jwt.JwtTokenUtil.TOKEN_HEADER;

/**
 * @Classname Yhk
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/10/3 15:32
 * @Created by y6522
 */
@RestController
@RequestMapping("/acl")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private PermissionService permissionService;


    @PostMapping("/user/list")
    public BaseResponse<IPage> list(@RequestBody UserInfoPageVo vo) {
        IPage page = userInfoService.selectUserInfoPage(vo);
        return BaseResponse.success(page);
    }

    @PostMapping("/user/save")
    public BaseResponse<String> userSave(@RequestBody UserInfoVo vo) {
        int flag = userInfoService.addUser(vo);
        if (flag > 0) {
            return BaseResponse.success("更新成功");
        } else {
            return BaseResponse.fail("更新失败");
        }
    }

    @PostMapping("/user/update")
    public BaseResponse<String> userUpdate(@RequestBody UserInfoVo vo) {
        int flag = userInfoService.updateUser(vo);
        if (flag > 0) {
            return BaseResponse.success("更新成功");
        } else {
            return BaseResponse.fail("更新失败");
        }
    }

    @PostMapping("/user/del")
    public BaseResponse<UserInfoPageVo> userDel(@RequestBody UserInfoVo vo) {
        int flag = userInfoService.delUser(vo.getId());
        if (flag > 0) {
            return BaseResponse.success("删除成功");
        } else {
            return BaseResponse.fail("删除失败");
        }
    }


    @PostMapping("/permission")
    public BaseResponse<UserInfoVo> permission(HttpServletRequest request) {
        String token = request.getHeader(TOKEN_HEADER);
        UserInfoVo userInfoVo = userInfoService.selectUserByUserName(JwtTokenUtil.getUserNameFromToken(token));
        BeanUtils.copyProperties(userInfoService.selectUserByUserId(userInfoVo.getId()), userInfoVo);
        userInfoVo.setRoleList(userInfoService.selectRoleByUserId(userInfoVo.getId()));
        userInfoVo.setPermissionList(permissionService.selectPermissionByUserId(userInfoVo.getId()));
        return BaseResponse.success(userInfoVo);
    }

    @PostMapping("/role/permission/list/{id}")
    public BaseResponse<List<PermissionVo>> list(@PathVariable Integer roleId) {
        return BaseResponse.success(permissionService.selectPermissionByRoleId(roleId));
    }

    @PostMapping("/role/permission/{id}")
    public BaseResponse<List<PermissionVo>> update(@PathVariable Integer id) {
        return BaseResponse.success(permissionService.selectPermissionByRoleId(id));
    }


}
